<template>
	<view>
    <!-- 搜索区域 -->
    <view class="search-box">
      <search @click="gotoSearch"></search>
    </view>
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :current="true">
			<swiper-item v-for="(item, i) in swiperList" :key="i">
				<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
					<image :src="item.image_src" mode=""></image>
				</navigator>
			</swiper-item>
		</swiper>
    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, i) in navList" :key="i" @click="cateClick(item)">
        <image :src="item.image_src" mode="widthFix"></image>
      </view>
    </view>
    <!-- 首页楼层区域 -->
    <view class="floor-list">
      <!-- 楼层标题 -->
      <view class="floor-title" v-for="(item, i) in floorList" :key="i">
        <image :src="item.floor_title.image_src" mode="widthFix"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-item">
          <!-- 楼层左边的图片 -->
          <navigator class="left-floor" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" mode="widthFix" :style="{width:item.product_list[0].image_width + 'rpx'}"></image>
          </navigator>
          <!-- 楼层右边的区域 -->
          <view class="right-floor">
            <navigator class="right-item" v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2 !== 0" :url="item2.url">
              <image :src="item2.image_src" mode="widthFix" :style="{width:item2.image_width + 'rpx'}"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 轮播图列表数据
				swiperList: [],
        // 分类导航的数据
        navList: [],
        // 楼层的数据列表
        floorList: []
			}
		},
		onLoad() {
			// 获取轮播图数据
			this.getSwiperList()
      // 获取分类导航数据
      this.getNavList()
      // 获取楼层数据列表
      this.getFloorList()
		},
		methods: {
      // 获取轮播图数据
			async getSwiperList () {
				const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata')
				console.log(res.message)
				if (res.meta.status !== 200) return uni.$msg()
				this.swiperList = res.message
			},
      // 获取分类导航数据
      async getNavList () {
        const { data: res } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) return uni.$msg()
        this.navList = res.message
      },
      // 点击分类跳转分类页面
      cateClick (item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 获取楼层数据列表
      async getFloorList () {
        const { data: res } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) return uni.$msg()
        // 给楼层的每个数据添加一个自定义url地址
        res.message.forEach(floor => {
          floor.product_list.forEach(pro => {
            pro.url = '/subpkg/goods_list/goods_list?' + pro.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },
      // 点击搜索栏跳转到搜索页面
      gotoSearch () {
        console.log(111)
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
		}
	}
</script>

<style lang="scss">
swiper{
  height: 330rpx;
  .swiper-item, image{
    width: 100%;
    height: 100%;
  }
}
.nav-list{
  display: flex;
  margin: 15px 0;
  justify-content: space-around;
  .nav-item, image{
    width: 128rpx;
    height: 140rpx;
  }
}
.floor-item{
  display: flex;
}
.right-floor{
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  padding-left: 10rpx;
}
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
}
</style>
